//
//  ForgetViewController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/16.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "ForgetViewController.h"
#import "addAdressCell.h"
#import "RevisePwdViewController.h"
#import "LoginStore.h"

@interface ForgetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *phoneName;
@property (nonatomic, copy) NSString *realPhone;
@property (nonatomic, copy) NSString *realCode;
@property (nonatomic, copy) NSString *code;

@end

@implementation ForgetViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self CleanSubView];
}
- (void)CleanSubView{
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    ViewHeader *header = [[ViewHeader alloc]initWithDefaultString:@"忘记密码"];
    [self.view addSubview:header];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, iPhoneX_Top, SCREEN_WIDTH, 100) style:UITableViewStyleGrouped];
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"addAdressCell" bundle:nil] forCellReuseIdentifier:@"addAdressCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    UIButton *tosure = [[UIButton alloc]init];
    tosure.backgroundColor = [UIColor colorWithHexString:@"0CA6F0"];
    tosure.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
    [tosure setTitle:@"下一步" forState:UIControlStateNormal];
    [tosure setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    [tosure addTarget:self action:@selector(tosure) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tosure];
    [tosure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(49);
        make.left.equalTo(self.view.mas_left).offset(14);
        make.right.equalTo(self.view.mas_right).offset(-14);
        make.top.equalTo(self.tableView.mas_bottom).offset(50);
    }];
}
- (void)sendCodeWith:(NSIndexPath *)index{
    [[[LoginStore alloc]init] ForgetsendCodeWithTelephoneNumber:self.phoneName Success:^(id  _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.realCode = res[@"data"][@"code"];
            self.realPhone = self.phoneName;
            addAdressCell *Cell = [self.tableView cellForRowAtIndexPath:index];
            [Cell daojishi];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)tosure{
    if (self.phoneName.length > 0) {
        if ([self.phoneName isEqualToString:self.realPhone]) {
            if (self.code.length > 0) {
                if ([self.code isEqualToString:self.realCode]) {
                    
                    RevisePwdViewController *VC = [[RevisePwdViewController alloc]init];
                    VC.phone = self.phoneName;
                    [[AppDelegate sharedAppDelegate] pushViewController:VC];
                }else{
                    [self showMBPError:@"请输入正确的验证码"];
                }
            }else{
                [self showMBPError:@"请输入验证码"];
            }
        }else{
            [self showMBPError:@"手机号与获取验证码手机号不一致"];
        }
    }else{
        [self showMBPError:@"请输入手机号码"];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    addAdressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addAdressCellID"];
    cell.rightarrow.hidden = YES;
    cell.detailtext.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (indexPath.row == 0) {
        cell.subtitle.text = @"手机号";
        cell.detailtext.placeholder = @"请输入手机号";
    }else if (indexPath.row == 1) {
        cell.detailtext.clearButtonMode = UITextFieldViewModeNever;
        cell.subtitle.text = @"验证码";
        cell.detailtext.placeholder = @"请输入短信验证码";
        cell.sendbutton.hidden = NO;
        [cell.sendbutton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [cell setSendBlock:^{
            if ([self.phoneName isVaildMobileNo]) {
                [self sendCodeWith:indexPath];
            }else{
                [self showMBPError:@"请输入正确的手机号"];
            }
        }];
    }
    [cell setDetailBlock:^(NSString * _Nonnull str) {
        if (indexPath.row == 0) {
            self.phoneName = str;
        }else if(indexPath.row == 1){
            self.code = str;
        }
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
